//接受web发送过来的返回事件
window.addEventListener("message", async function (event) {
    //处理页面响应的消息
    if (event.data.from === "web") {
        if (event.data.action == "localStorage_get") {
            await session.set("localStorage", event.data.data);
            // console.log("rev web response: " + JSON.stringify(event.data))
        }
        // console.log("rev web response: " + JSON.stringify(event.data))
    } else {
        if (!!event.data && !!event.data.from && event.data.from == 'extenstion') {

        } else {
            try {
                if (event.data.data.request.api.indexOf("meiri56") == -1) {
                    console.log(event)
                }
            } catch (e) {
                console.log(event)
            }
        }
    }

});

function webError() {
    if (document.body.innerHTML.indexOf('geo.captcha-delivery.com/captcha') != -1) {
        return true;
    }
    return false;
}

let images = []
//接受worker.js popup.js
chrome.runtime.onMessage.addListener(async function (request, sender, sendResponse) {
    if (request.type == 'heartBeat') {
        let pluginName = await session.get("pluginName", null);
        let hostCode = await session.get("hostCode", null);
        let serverName = await session.get("serverName", null);
        let res = core.heartBeat(pluginName, hostCode, serverName);

        await session.set("heartBeat_time", new Date().getTime())

        let tokenRunning = await session.get("TOKEN_RUNNING", false)
        let stopService = await session.get("stopService", false)
        if (!tokenRunning && !stopService && !webError()) {
            await session.set("TOKEN_RUNNING", true)
            await getAccountAndRefresh();
        }
        sendResponse(res);
    }
    if (request.type == 'reload' || request.type == 'init') {
        if (!webError()) {
            window.location.reload();
        }
    }
    if (request.type == 'getAccount') {
        await getAccountAndRefresh();
        sendResponse("getAccount");
    }
    if (request.type == 'image') {
        console.log(request.data)
        images.push(request.data)
        if (images.length >= 2) {
            let dis = core.sliderCode({
                img1: images.filter(item => item.indexOf("frag.png") == -1)[0],
                img2: images.filter(item => item.indexOf("frag.png") != -1)[0],
            })
            images = [];
            console.log(dis);
            let sliders = document.getElementsByClassName("slider");
            if (!!sliders && sliders.length > 0 && !!dis && dis > 0) {
                document.getElementsByClassName("slider")[0].dispatchEvent(new MouseEvent('mousedown', {
                    'bubbles': true,
                    'cancelable': true,
                    'clientX': 0
                }))
                document.getElementsByClassName("slider")[0].dispatchEvent(new MouseEvent('mousemove', {
                    'bubbles': true,
                    'cancelable': true,
                    'clientX': 0 + dis - 2
                }))

                document.getElementsByClassName("slider")[0].dispatchEvent(new KeyboardEvent('mouseup', {
                    'bubbles': true,
                    'cancelable': true
                }))
            }
        }
    }
    return true;
});

//current_step = getAccountAndRefresh
async function getAccountAndRefresh() {
    let stopService = await session.get("stopService", false)
    if (stopService) {
        return;
    }

    await session.set("msg", "等待5秒")
    await localTimeFunction(async function () {
        console.log("getAccountAndRefresh: 开始获取账号")
        await session.set("msg", "开始获取账号")
        await session.set("TOKEN_RUNNING", true)
        await session.remove("account");
        await session.remove("loginStatus");
        await session.remove("hasDoneLogin");
        await session.set("current_step", "getAccountAndRefresh");
        await session.set("current_step_time", new Date().getTime());
        let last_account = await session.get("last_account", null)

        let pluginName = await session.get("pluginName", null);
        let hostCode = await session.get("hostCode", null);
        let serverName = await session.get("serverName", null);

        let hasGetAccount = await session.get("hasGetAccount", false);
        let hasGetAccount_time = await session.get("hasGetAccount_time", 0);
        if (!hasGetAccount || new Date().getTime() - hasGetAccount_time > 30000) {
            let res = core.getAccount(pluginName, hostCode, serverName);
            await session.set("hasGetAccount", true);
            await session.set("hasGetAccount_time", new Date().getTime());

            if (!!res && !!res['username']) {
                console.log("getAccountAndRefresh: 账号: " + res['username'])
                await session.set("msg", "获取账号:" + res['username'])
                if (!!last_account && last_account == res['username']) {
                    console.log("getAccountAndRefresh: 与上次账号重复: " + res['username'])
                    await session.set("msg", "与上次账号重复")
                    await localTimeFunction(async function () {
                        await getAccountAndRefresh();
                    }, 2000)
                    return;
                } else {
                    console.log("getAccountAndRefresh: refreshToken " + res['username'])
                    await session.set("msg", "开始设置新账号cookie")
                    await session.set("account", res)

                    await session.set("current_step", "refreshToken");
                    await session.set("current_step_time", new Date().getTime());
                    await session.remove("hasGetAccount");
                }
            } else {
                console.log("getAccountAndRefresh: 未获取到账号")
                await session.set("msg", "未获取到账号")
                await localTimeFunction(async function () {
                    await session.remove("hasGetAccount");
                    await getAccountAndRefresh();
                }, 2000)
            }
        }
    }, 5000);
}

setInterval(async function () {
    let current_step = await session.get("current_step", null);
    let current_step_time = await session.get("current_step_time", null);
    let account = await session.get("account", null)
    let done_refreshToken = await session.get("done_refreshToken", false)
    let done_refreshToken_time = await session.get("done_refreshToken_time", 0)
    let stopService = await session.get("stopService", false)
    if (current_step == "refreshToken" && !!account && (!done_refreshToken || new Date().getTime() - done_refreshToken_time > 30000) && !stopService) {
        await session.set("done_refreshToken", true);
        await session.set("done_refreshToken_time", new Date().getTime());
        await refreshToken(account);
    }
    if(current_step == "refreshToken_tmp" && !!current_step_time && new Date().getTime() - current_step_time > 15000){
        await session.set("current_step", "refreshToken");
        await session.set("current_step_time", new Date().getTime());
    }
}, 3000)

//current_step = refreshToken
async function refreshToken(res) {

    let otherCache = (!!res && !!res['otherCache']) ? JSON.parse(res['otherCache']) : null;
    if (!!otherCache && !!otherCache['cookies'] && !!otherCache['localStorage']) {
        //再增加

        let message = {"from": "extenstion", "action": "localStorage_set", data: otherCache['localStorage']};
        window.postMessage(message, "*");
        sendMsg({action: 'setCookie', data: otherCache['cookies']})

        console.log("设置完cookie: " + res['username'])
        await session.set("msg", "重新设置完新账号cookie到浏览器域")


        await localTimeFunction(async function () {
            console.log("刷新: " + res['username'])
            await session.set("current_step", "checkLogin");
            await session.set("current_step_time", new Date().getTime());
            await session.remove("done_refreshToken");
            await session.remove("resetToken")
            window.location.reload();
        }, 2000)

    } else {

        sendMsg({action: 'delCookie', domain: 'www.cma-cgm.com', data: [".AspnetCookie"]})
        console.log("删除old token : " + res['username'])

        let resetToken = await session.get("resetToken", 0);
        if (new Date().getTime() - resetToken > 60000 * 2) {
            await session.set("current_step", "refreshToken_tmp");
            await session.set("current_step_time", new Date().getTime());
            await localTimeFunction(async function () {
                await session.set("msg", "删除历史token")
                var time = new Date().getTime();
                let noLoginCookie = {
                    "localStorage": [{"key": "wm-state", "val": "{\"currentFlow\":{}}"}, {
                        "key": "walkMe_wm-po-p",
                        "val": "{\"val\":\"2\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "ABTastyData",
                        "val": "{\"VisitedPages\":[{\"url\":\"https://www.cma-cgm.com/\",\"visite\":1,\"time\":" + time + ",\"title\":\"CMA CGM | A global player in sea, land, air, and logistics solutions.\"},{\"url\":\"https://www.cma-cgm.com/mycmacgm\",\"visite\":1,\"time\":" + time + ",\"title\":\"CMA CGM | My CMA CGM\"},{\"url\":\"https://www.cma-cgm.com/mycmacgm\",\"visite\":1,\"time\":" + time + ",\"title\":\"CMA CGM | My CMA CGM\"},{\"url\":\"https://www.cma-cgm.com/\",\"visite\":1,\"time\":" + time + ",\"title\":\"CMA CGM | A global player in sea, land, air, and logistics solutions.\"}],\"transactions\":[{\"id\":\"P00759629\",\"value\":1,\"shipping\":\"CCGID\",\"affiliation\":\"CCGID\",\"time\":" + time + "},{\"id\":\"P00759629\",\"value\":1,\"shipping\":\"CCGID\",\"affiliation\":\"CCGID\",\"time\":" + time + "}]}"
                    }, {
                        "key": "wm-deepui-lexicon-enabled-for-data-file",
                        "val": "true"
                    }, {
                        "key": "walkMe_wm-customer-hub-isInSetup",
                        "val": "{\"val\":\"\\\"N\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-ueuT",
                        "val": "{\"val\":\"\\\"P00759629\\\"\",\"exp\":" + (time + 60000 * 60 * 6) + "}"
                    }, {"key": "da3c5c4f68d04e3ebe7826faa1e14530priority", "val": "2"}, {
                        "key": "walkMe_wm-ds-b",
                        "val": "{\"val\":\"\\\"[]\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-customer-hub-wave",
                        "val": "{\"val\":\"\\\"-\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-ds-lb",
                        "val": "{\"val\":\"\\\"{}\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-customer-hub-cluster",
                        "val": "{\"val\":\"\\\"-\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-cseu-id",
                        "val": "{\"val\":\"\\\"b88abc5e-12c9-45e6-bec9-ed23d16308a6\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {"key": "wm-deepui-master-mode", "val": "true"}, {
                        "key": "walkMe_wm-ASRep-14-562197",
                        "val": "{\"val\":\"1\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "notifalert",
                        "val": "{\"key\":\"notifalert\",\"expirationDate\":\"2024-07-18T16:06:33.856Z\",\"data\":{\"TotalUnread\":0}}"
                    }, {
                        "key": "walkMe_wm-flw-preplay",
                        "val": "{\"val\":\"null\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-ds-lbb",
                        "val": "{\"val\":\"\\\"{}\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-ds-s",
                        "val": "{\"val\":\"\\\"[]\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-ueug",
                        "val": "{\"val\":\"\\\"P00759629\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-deepui-end-user-rand",
                        "val": "{\"val\":\"0.795771849626824\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-wmv",
                        "val": "{\"val\":\"\\\"P00759629\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-flow-stack",
                        "val": "{\"val\":\"{\\\"flows\\\":[],\\\"playInitiator\\\":{\\\"type\\\":6},\\\"isFirstVisualStep\\\":true,\\\"playGuid\\\":\\\"1721318774586-1844532-b32f39c7-5a62-4ea7-b3c0-a1d3c94a2df3\\\"}\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-stpcond-14-562197-x",
                        "val": "{\"val\":\"1\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-po-q",
                        "val": "{\"val\":\"{\\\"1\\\":1721318658782}\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-ds-lbp",
                        "val": "{\"val\":\"\\\"[]\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-deepui-user-mapping-updated-at",
                        "val": "{\"val\":\"1721301180239\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "SessionTimestamp",
                        "val": "Fri Jul 19 2024 00:06:13 GMT+0800 (中国标准时间)"
                    }, {
                        "key": "walkMe_wm-ds-lfb",
                        "val": "{\"val\":\"\\\"{}\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-smtp-init",
                        "val": "{\"val\":\"{\\\"type\\\":6}\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-ds-hb",
                        "val": "{\"val\":\"\\\"[]\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-po-r",
                        "val": "{\"val\":\"2\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-customer-hub-segment",
                        "val": "{\"val\":\"\\\"-\\\"\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {
                        "key": "walkMe_wm-session-per-user",
                        "val": "{\"val\":\"{\\\"P00759629\\\":{\\\"euId\\\":\\\"P00759629\\\",\\\"sId\\\":\\\"251002b4-4035-41c0-b3bc-6bdbae7d616a\\\",\\\"startTime\\\":" + time + ",\\\"lastActivityTime\\\":" + time + ",\\\"parentSId\\\":\\\"3f8b41fb-20d1-4a05-a3ed-9d6d17155b03\\\"},\\\"0a9d3eb0-a793-4a85-8ed2-bac5694f0d31\\\":{\\\"euId\\\":\\\"0a9d3eb0-a793-4a85-8ed2-bac5694f0d31\\\",\\\"sId\\\":\\\"680b8d4e-75fc-4ce3-b20c-c302d534ef6b\\\",\\\"startTime\\\":" + time + ",\\\"lastActivityTime\\\":" + time + ",\\\"parentSId\\\":\\\"3f8b41fb-20d1-4a05-a3ed-9d6d17155b03\\\"}}\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }, {"key": "wm-preload-data", "val": "true"}, {
                        "key": "walkMe_wm-hb",
                        "val": "{\"val\":\"{\\\"sendBaseTime\\\":1721318674254}\",\"exp\":" + (time + 60000 * 60 * 24) + "}"
                    }],
                    "cookies": [{
                        "domain": "auth.cma-cgm.com",
                        "hostOnly": true,
                        "httpOnly": false,
                        "name": "pf-accept-language",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "en"
                    }, {
                        "domain": "auth.cma-cgm.com",
                        "hostOnly": true,
                        "httpOnly": false,
                        "name": "_pk_ref.21.d201",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "%5B%22%22%2C%22%22%2C1721316225%2C%22https%3A%2F%2Fwww.cma-cgm.com%2F%22%5D"
                    }, {
                        "domain": "auth.cma-cgm.com",
                        "hostOnly": true,
                        "httpOnly": false,
                        "name": "_pk_ses.21.d201",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "1"
                    }, {
                        "domain": "auth.cma-cgm.com",
                        "hostOnly": true,
                        "httpOnly": false,
                        "name": "_pk_id.21.d201",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "3321f14a89ace9c0.1721228871."
                    }, {
                        "domain": "auth.cma-cgm.com",
                        "hostOnly": true,
                        "httpOnly": true,
                        "name": "PF",
                        "path": "/",
                        "sameSite": "no_restriction",
                        "secure": true,
                        "session": true,
                        "storeId": "0",
                        "value": "B6BDY0WBjVQcsHih6sXfRgkVlTF9Ga6QGxBsnsfUfMDA"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000,
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "_pk_id.1.d313",
                        "path": "/",
                        "sameSite": "lax",
                        "secure": false,
                        "session": false,
                        "storeId": "0",
                        "value": "321c39701d7d5e7b.1721318659."
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000,
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "_pk_ses.1.d313",
                        "path": "/",
                        "sameSite": "lax",
                        "secure": false,
                        "session": false,
                        "storeId": "0",
                        "value": "1"
                    }, {
                        "domain": "www.cma-cgm.com",
                        "hostOnly": true,
                        "httpOnly": false,
                        "name": "Human_Search",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "1"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000 + ".028355",
                        "hostOnly": false,
                        "httpOnly": true,
                        "name": "MustRelease",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": true,
                        "session": false,
                        "storeId": "0",
                        "value": "28.1.2.0"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000,
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "_cs_c",
                        "path": "/",
                        "sameSite": "no_restriction",
                        "secure": true,
                        "session": false,
                        "storeId": "0",
                        "value": "0"
                    }, {
                        "domain": ".cma-cgm.com",
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "rxVisitor",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "1721316222174CAMJ20DVOKPV49V4IO4J5A0ADB6GI8G7"
                    }, {
                        "domain": ".cma-cgm.com",
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "dtLatC",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "252"
                    }, {
                        "domain": ".cma-cgm.com",
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "rxvt",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": (time) + "|" + (time + 60000 * 60 * 24)
                    }, {
                        "domain": ".cma-cgm.com",
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "dtPC",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "5$518671001_528h-vEQMBMPHWRISBRCAKOPTFBDPRLIKAMKFG-0"
                    }, {
                        "domain": ".cma-cgm.com",
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "dtSa",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "false%7C_load_%7C2%7C_onload_%7C-%7C1721318671069%7C518671001_528%7Chttps%3A%2F%2Fauth.cma-cgm.com%2Fas%2Fauthorization.oauth2%3Fclient_5Fid%3Dwebapp-must%26redirect_5Furi%3Dhttps_253A_252F_252Fwww.cma-cgm.com_252Fsignin-oidc%26response_5Ftype%3Dcode%26scope%3Demail_2520openid_2520profile_2520Ecom_253Awebapp-must-apl-anl-cnc_2520ans_253Afe_253Aread_2520ans_253Afe_253Awrite%26code_5Fchallenge%3DpPSCRx3gMJ05gX7AXeBirUZRVZztFxlKwe4DurJK5QM%26code_5Fchallenge_5Fmethod%3DS256%26state%3DOpenIdConnect.AuthenticationProperties_253DQTHAUf7B8CIEjhByORGRwK3KadzG-TmI3FDh5y42PNbGARr9vY4Go76Fpu-UOHGJknPAcJZfrwxBCo0M9uA_5FI-cQ7NISKa8tjzwDUZz-NflzoxILoThQmbVlqk_5F1mbGVJOolHt3r7AehKqiXEBB6YrtkcpnLYkZJ88d8pl-Oe_5FgIuQTE5HWQxej6GRPgF08xm27BvbjsHdRXLGnGfWWiCImWNtNYpMfq-OZZv40U1EyjYIrtSRGZMcPq7yLBCMhGM3AAru0S-ev1Le0GpKbepz2bOYvzhxaZVdBRThyRk95ZzbuJCRb0JVuSbpaeLdJJ_2526Language_253Den-US_2526actas_253Dfalse%26response_5Fmode%3Dform_5Fpost%26x-client-SKU%3DID_5FNET472%26x-client-ver%3D6.27.0.0%7CSubmit%20Form%7C1721318671001%7C%7C"
                    }, {
                        "domain": ".cma-cgm.com",
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "dtCookie",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": true,
                        "storeId": "0",
                        "value": "v_4_srv_5_sn_2475AA80D29153DE549ED731B7F9AAEB_perc_100000_ol_0_mul_1_app-3A0b422508580a7b79_0_app-3Ac276214421dd42e9_0_rcs-3Acss_0"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000 + ".585203",
                        "hostOnly": false,
                        "httpOnly": true,
                        "name": "HasBeenConnected",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": true,
                        "session": false,
                        "storeId": "0",
                        "value": "1"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000,
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "ABTastySession",
                        "path": "/",
                        "sameSite": "lax",
                        "secure": false,
                        "session": false,
                        "storeId": "0",
                        "value": "mrasn=&lp=https%253A%252F%252Fwww.cma-cgm.com%252F"
                    }, {
                        "domain": "www.cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000 + ".619364",
                        "hostOnly": true,
                        "httpOnly": false,
                        "name": "AWSALBTG",
                        "path": "/",
                        "sameSite": "unspecified",
                        "secure": false,
                        "session": false,
                        "storeId": "0",
                        "value": "7tiD86nCCW1h7Yu5+9FIDrFCx6ZF4YScZCShPxR2HMr79HuDkP/YQNUuCY64KtjyV6sQdPUEs15E+2IOu48ptjk3U9PZqwo7k4yopdxz222PuRypyZYPoDSqHzjU+SxH55bh/zJ2o2OLqwo1p/dSIJ7B3M1ps2LNRd2nvS98JiO4"
                    }, {
                        "domain": "www.cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000 + ".619531",
                        "hostOnly": true,
                        "httpOnly": false,
                        "name": "AWSALBTGCORS",
                        "path": "/",
                        "sameSite": "no_restriction",
                        "secure": true,
                        "session": false,
                        "storeId": "0",
                        "value": "7tiD86nCCW1h7Yu5+9FIDrFCx6ZF4YScZCShPxR2HMr79HuDkP/YQNUuCY64KtjyV6sQdPUEs15E+2IOu48ptjk3U9PZqwo7k4yopdxz222PuRypyZYPoDSqHzjU+SxH55bh/zJ2o2OLqwo1p/dSIJ7B3M1ps2LNRd2nvS98JiO4"
                    }, {
                        "domain": ".www.cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000,
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "OptanonConsent",
                        "path": "/",
                        "sameSite": "lax",
                        "secure": false,
                        "session": false,
                        "storeId": "0",
                        "value": "isGpcEnabled=0&datestamp=Fri+Jul+19+2024+00%3A06%3A25+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=202308.1.0&browserGpcFlag=0&isIABGlobal=false&hosts=&genVendors=&consentId=c9c704d2-6dbf-4089-b70c-100af3c7a8c0&interactionCount=1&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1%2CC0004%3A1&AwaitingReconsent=false"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000,
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "ABTasty",
                        "path": "/",
                        "sameSite": "lax",
                        "secure": false,
                        "session": false,
                        "storeId": "0",
                        "value": "uid=hsr2z8txwrrts94g&fst=1721318665880&pst=-1&cst=1721318665880&ns=1&pvt=4&pvis=4&th="
                    }, {
                        "domain": ".cma-cgm.com",
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "_cs_cvars",
                        "path": "/",
                        "sameSite": "no_restriction",
                        "secure": true,
                        "session": true,
                        "storeId": "0",
                        "value": "%7B%221%22%3A%5B%22partnerCode%22%2C%22undefined%22%5D%2C%222%22%3A%5B%22userId%22%2C%22undefined%22%5D%2C%223%22%3A%5B%22hostname%22%2C%22www.cma-cgm.com%22%5D%2C%224%22%3A%5B%22sessionKey%22%2C%22aa81a5fc-4fd7-aeab-8807-40f7b0612da2.1%22%5D%7D"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000,
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "_cs_id",
                        "path": "/",
                        "sameSite": "no_restriction",
                        "secure": true,
                        "session": false,
                        "storeId": "0",
                        "value": "aa81a5fc-4fd7-aeab-8807-40f7b0612da2.1721318658.1.1721318785.1721318658.1640190127.1755482658867.1"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000,
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "_cs_s",
                        "path": "/",
                        "sameSite": "no_restriction",
                        "secure": true,
                        "session": false,
                        "storeId": "0",
                        "value": "6.0.0.1721320585810"
                    }, {
                        "domain": ".cma-cgm.com",
                        "expirationDate": (time + 60000 * 60 * 24) / 1000 + ".677366",
                        "hostOnly": false,
                        "httpOnly": false,
                        "name": "datadome",
                        "path": "/",
                        "sameSite": "lax",
                        "secure": true,
                        "session": false,
                        "storeId": "0",
                        "value": "CkTVI8c5I1KYyhIXtUxkQrw8bu6CqcuBaKl_lnaUmgg4Zy5QJQZe70QloijKlihJmwnlV4zPdx6Ly5YVrO_adc0HdzCpoEsK3JeYMX~VF8_NnJhTTEudtcbVQiroF~Ze"
                    }]
                }
                //set 无效cookie等同于删除cookie，cma删除cookie会触发风控
                message = {"from": "extenstion", "action": "localStorage_set", data: noLoginCookie['localStorage']};
                window.postMessage(message, "*");
                sendMsg({action: 'setCookie', data: noLoginCookie['cookies']})

                await localTimeFunction(async function () {
                    await session.set("msg", "历史token清理完毕，刷新")
                    await session.set("current_step", "refreshToken");
                    await session.remove("done_refreshToken");
                    await session.set("resetToken", new Date().getTime())
                    window.location.reload();
                    return;
                }, 2000)

            }, 1000)
        }

        let current_step = await session.get("current_step", null);
        if (current_step != "refreshToken") {
            return;
        }

        await localTimeFunction(async function () {
            console.log("没有othercache，需要登陆")
            await session.set("msg", "准备登陆")
            await session.set("current_step", "login");
            await session.set("current_step_time", new Date().getTime());
            await session.remove("done_refreshToken");
            await session.remove("resetToken")
            window.location.reload();
        }, 2000)
    }

}